<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");
// //跨域
// header("Access-Control-Allow-Credentials: true");
// header("Access-Control-Allow-Origin: *");
// //CORS
// header("Access-Control-Request-Methods:GET, POST, PUT, DELETE, OPTIONS");
// header('Access-Control-Allow-Headers:x-requested-with,content-type,test-token,test-sessid');


$name = $email = $password = "";

// 校验是否为空
if(Check::isEmpty($_POST["name"])) {
    exit(Result::jsonReturn(1, "名称不得为空"));
}
if(Check::isEmpty($_POST["password"])) {
    exit(Result::jsonReturn(1, "密码不得为空"));
}

// 校验是否符合格式
if(Check::isName($_POST["name"])) {
    $name = $_POST["name"];
}else {
    exit(Result::jsonReturn(1, "用户名格式有误"));
}
if(Check::isPassword($_POST["password"])) {
    $password = Func::encipher($_POST["password"]);
}else {
    exit(Result::jsonReturn(1, "密码格式错误"));
}

// 查询数据库
$db = new DB();
$sql = "SELECT * FROM qilian_users WHERE name='{$name}'";
$res = array();
if($res = $db->getOne($sql)) {
    if($res["password"] == $password) {
        session_start();
        $_SESSION["id"] = $res["id"];
        $_SESSION["name"] = $res["name"];
        $_SESSION["email"] = $res["email"];
        $_SESSION["power"] = $res["power"];
        exit(Result::jsonReturn(0, "已登录", array(
            "id"=>$res["id"],
            "name"=>$res["name"], 
            "email"=>$res["email"], 
            "power"=>$res["power"]))); 
    }else {
        exit(Result::jsonReturn(1, "密码错误"));
    }
}else {
    exit(Result::jsonReturn(1, "没有此用户"));
}

?>